import 'dart:io';

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:umeng_vertify/result_value.dart';
import 'package:umeng_vertify/umeng_vertify.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  bool canClick = false;

  StreamSubscription? stream;

  @override
  void initState() {
    super.initState();
    UmengVertify.addListener((call) async {
      switch (call.method) {
        case "wechatLogin":
          //月野兔微信登录
          Future.delayed(Duration(seconds: 2), () {
            UmengVertify.loginField();
          });
          break;
        case "appleLogin":
      //apple登录
        Future.delayed(Duration(seconds: 2), () {
          UmengVertify.loginField();
        });
        break;
        case "touristsBrowse":
        //随便逛逛
          Future.delayed(Duration(seconds: 2), () {
            UmengVertify.loginField();
          });
          break;
      }
      return true;
    });
    UmengVertify.initHelper().then((value) async {
      Future.wait([
        UmengVertify.setAuthSDKInfo(
            "Q6UGzZk12ZvVqsD0TNB6IahZ6O6+BEhX7F3mEUPovpwzyhZEwgZ2Kv+GFBPURPcE46fXDZGS6mhEJ9hTSr+J30MhiEIhMsVce6kgkmw0YcIQ7e0d2WzlYq53gJMbNQKESIK56AUPs3aOWLUNCMk+289X+fBHC6oIWjEfi43HsSTZnxEH0f6lqpvp6g5g2SgIjOXjMeeD2fswGotJxLVOsBD1Z+nHqNC58/aXOdpxJuMzvEZ0kUaCpzGFfHxCY6P35pJPXrPHGZ5GH0rQlYw61F0l8EQVcWQJ+FZFYIuB2mGndp65QRfV0hLM3V4Bj6FiQrO0l4Uw9WM="),
        UmengVertify.checkEnvAvailable(),
        UmengVertify.addAuthRegisterXmlConfig(),
        UmengVertify.getIOSVerifyViewModel()
      ]);
    }).whenComplete(() {
      canClick = true;
      setState(() {});
    });
    stream = UmengVertify.event.receiveBroadcastStream().listen((event) {
      UmengVertify.getData(event, umTokenDataGet: (value) {
        // ResultValue? resultValue = ResultValue.checkValue(int.parse(value["type"]));
        ResultValue? resultValue = value.resultType;

        if(resultValue == null) return;
        switch (resultValue) {
          case ResultValue.getTokenSuccess:
            print("数据的token");

            print("数据的token${value.uMTokenData.token}");
            print("数据的requestId${value.uMTokenData.requestId}");
            UmengVertify.showCloseButton();
            UmengVertify.quitLoginPage();
            break;
          case ResultValue.getTokenError:
            break;
          case ResultValue.deviceCheckSuccess:
            UmengVertify.getLoginToken();
            break;
        }
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    if (Platform.isAndroid) {
      SystemChrome.setSystemUIOverlayStyle(
          SystemUiOverlayStyle(statusBarColor: Colors.transparent));
    }

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Text('Running on: $_platformVersion\n'),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            UmengVertify.getLoginToken();
          },
        ),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    stream?.cancel();
  }
}
